package lyh.hnu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lyh.hnu.common.CustomException;
import lyh.hnu.common.R;
import lyh.hnu.entity.po.Category;
import lyh.hnu.entity.po.Dish;
import lyh.hnu.entity.po.Setmeal;
import lyh.hnu.mapper.CategoryMapper;
import lyh.hnu.service.CategoryService;
import lyh.hnu.service.DishService;
import lyh.hnu.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService{

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public R<String> delete(long ids) {
        long dishCount = dishService.lambdaQuery().eq(Dish::getCategoryId, ids).count();
        long setmealCount = setmealService.lambdaQuery().eq(Setmeal::getCategoryId, ids).count();

        if (dishCount > 0 || setmealCount > 0) {
            throw new CustomException("当前分类已关联菜品或套餐，无法删除");
            //return R.error("当前分类已关联菜品或套餐，无法删除");
        }
        removeById(ids);
        return R.success("删除分类成功");
    }
}
